package com.sgb.kjwl.viewmodel.enterprise;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.enterprise.TeamIntelligenceEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.fragment.enterprise.TeamIntelligenceFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TeamIntelligenceModel extends BaseViewModel {
    private TeamIntelligenceFragment fragment;
    int pageNum = 1;

    public TeamIntelligenceModel(TeamIntelligenceFragment fragment) {
        this.fragment = fragment;
    }

    /**
     *
     * @param isLodMore
     * @param teamNo
     */
    public void getTeamIntelligenceList(boolean isLodMore, String teamNo) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getQualification(teamNo, pageNum, 3).enqueue(new Callback<BaseEntity<ListResponse<List<TeamIntelligenceEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TeamIntelligenceEntity>>>> call, Response<BaseEntity<ListResponse<List<TeamIntelligenceEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showList(response.body().getData().getList(), isLodMore);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TeamIntelligenceEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}